/**
 * 
 */
package lib;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import lib.ConnectionWeb;
import org.htmlcleaner.TagNode;

/**
 * @author Dinh
 *
 */
public class StringEdit {
	
	public StringEdit() {
	};
	
	public static String EditXpath(String s)
	{
		String t="";
		if(s.indexOf("html")!=-1)
		t=s.substring(s.indexOf("html")+4,s.length());
		else t=s;
		return t;
	}
	
	public static String EditLink(String url, String link)
	{	String tl=link; 
		if(link.indexOf("http")==-1) link= url+link;
		

		link=link.replace("&amp;","&" );
		link=link.replace("../","" );
		ConnectionWeb con= new ConnectionWeb();
		boolean flag= con.TryConnect(link);
		if(flag==true) return link;
		else
		{
			try {
				URL u= new URL(url);
				String tu=u.getHost();
				link="http://"+tu+"/"+tl;
				flag=con.TryConnect(link);
				if(flag==true) return link;
				else
				{
					link="http://"+tu+tl;
				}
				flag=con.TryConnect(link);
				if(flag==true) return link;
				else
				{
					return null;
				}
						
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

	}
	
	public static String GetIdString(TagNode t)
	{	String s="";
		List<?> ln=t.getAllElementsList(false);
		for(int i=0;i<ln.size();i++)
		{
			TagNode t2= (TagNode) ln.get(i);
			List<?> ln2=t2.getAllElementsList(false);
			if(ln2.size()==0)
			{	
				s+=t2.getText().toString();				
			}
			else
			{
				for(int j=0;j<ln2.size();j++)
				{
					TagNode t3= (TagNode) ln2.get(j);
					List<?> ln3=t3.getAllElementsList(false);
					if(ln3.size()==0)
					{
						s+=t3.getText().toString();
					}
					else{
							
							for(int k=0;k<ln3.size();k++)
							{
								TagNode t4= (TagNode) ln3.get(k);
									s+=t4.getText().toString();
								
							}
						}
				}			
			}
			
		}
		
		return s;
	}
	
	public static String EditDir(String t)
	{
		t=t.replace("\n","");
		t=t.replace("\r", "");
		t= t.replaceAll("\\s+", " ");
		t=t.trim();
		t=t.replace("&amp;","&" );
		t=t.replaceAll("&#8211", "-");
		return t;
	}
	public static String duyetNode(TagNode t,String s)
	{
		if(t.hasChildren()==false)
			return s+t.getText().toString();
		else
		{
			List<?> ln=t.getAllElementsList(false);
			for(int i=ln.size()-1;i>=0;i--)
			{
				s+=duyetNode((TagNode)ln.get(i),s);
			}
			
			return s;
		}
	}
	
	public static String ChuanHoa(String t)
	{
	/*	while(t.charAt(0) ==' ')
		{
			t=t.substring(1, t.length());
		}
		while(t.charAt(t.length()-1)==' ')
		{
			
		}*/
		t= t.trim();
		
		return t;
	}
	public static String removeNotNum(String t)
	{
	/*	while(t.charAt(0) ==' ')
		{
			t=t.substring(1, t.length());
		}
		while(t.charAt(t.length()-1)==' ')
		{
			
		}*/
		if(t!=null)
		{
		t= t.replace("[", "");
		t= t.replace("]", "");
		return t;
		}
		
		return t;
	}
	
}
